/*
 * @Description:
 * @Author: yyh
 * @Date: 2022-03-12 17:43:25
 * @LastEditors: yyh
 * @LastEditTime: 2022-05-16 11:04:26
 */
var db = require("../utils/db");
var url = require("url");
var qs = require("querystring");

//新增资产数据
addAssetsInfo = async (req, res) => {
  var {
    asset_id,
    asset_name,
    classification,
    unit,
    price,
    supplier,
    register_time,
    status
  } = req.body;
  var sql =
    "insert into asset (asset_id,asset_name,classification,unit,price,supplier,register_time,status) values(?,?,?,?,?,?,?,?)";
  var sqlArr = [
    asset_id,
    asset_name,
    classification,
    unit,
    price,
    supplier,
    register_time,
    status
  ];
  var result = await db.SySqlConnect(sql, sqlArr);
  if (result.affectedRows == 1) {
    res.send({
      code: 200,
      msg: "新增成功"
    });
  } else {
    res.send({
      code: 400,
      msg: "新增失败"
    });
  }
};

//删除资产数据
deleteAssets = async (req, res) => {
  var asset_id = req.query[0];
  var sql = "delete from asset where asset_id = ? ";
  var sqlArr = [asset_id];
  var result = await db.SySqlConnect(sql, sqlArr);
  if (result.affectedRows == 1) {
    res.send({
      code: 200,
      msg: "删除成功"
    });
  } else {
    res.send({
      code: 400,
      msg: "删除失败"
    });
  }
};

//修改资产数据
updateAssetsInfo = async (req, res) => {
  var {
    asset_id,
    asset_name,
    classification,
    unit,
    price,
    supplier,
    register_time,
    status
  } = req.body;
  var sql =
    "update asset set asset_name=?,classification=?,unit=?,price=?,supplier=?,register_time=?,status=? where asset_id=?";
  var sqlArr = [
    asset_name,
    classification,
    unit,
    price,
    supplier,
    register_time,
    status,
    asset_id
  ];
  var result = await db.SySqlConnect(sql, sqlArr);
  if (result.affectedRows == 1) {
    res.send({
      code: 200,
      msg: "修改成功"
    });
  } else {
    res.send({
      code: 400,
      msg: "修改失败"
    });
  }
};

//修改资产状态
updateAssetsStatus = async (req, res) => {
  var {
    asset_id,
    status
  } = req.body;
  var sql =
    "update asset set status=? where asset_id=?";
  var sqlArr = [
    status,
    asset_id
  ];
  var result = await db.SySqlConnect(sql, sqlArr);
  if (result.affectedRows == 1) {
    res.send({
      code: 200,
      msg: "修改成功"
    });
  } else {
    res.send({
      code: 400,
      msg: "修改失败"
    });
  }
};


//统计不同资产状态的数量
getAssetsStatusTotal = async (req, res) => {
  var { status } = req.query;
  var sql = "select count(*) as result from asset where status=?";
  var sqlArr = [status];
  var callBack = async (err, data) => {
    if (err) {
      console.log(err);
      res.send({
        code: 400,
        msg: "查询失败"
      });
    } else {
      res.send({
        code: 200,
        msg: "查询成功",
        data: data
      });
    }
  };
  db.sqlConnect(sql, sqlArr, callBack);
}

//模糊查询、分页查询资产数据
getAssetsInfo = async (req, res) => {
  var params = url.parse(req.url).query;
  params = qs.parse(params);
  var sql = "select * from asset";
  var sqlArr = [];
  var isMore = false; //是否有多个查询参数
  if (params.asset_id) {
    sql += " where asset_id like ?";
    sqlArr.push("%" + params.asset_id + "%");
    isMore = true;
  }
  if (params.classification) {
    if (isMore) {
      sql += " and classification like ?";
    } else {
      sql += " where classification like ?";
      isMore = true;
    }
    sqlArr.push("%" + params.classification + "%");
  }
  if (params.status) {
    if (isMore) {
      sql += " and status like ?";
    } else {
      sql += " where status like ?";
      isMore = true;
    }
    sqlArr.push("%" + params.status + "%");
  }
  if (params.startTime && params.endTime) {
    if (isMore) {
      sql += " and register_time between ? and ?";
    } else {
      sql += " where register_time between ? and ?";
    }
    sqlArr.push(params.startTime, params.endTime);
  }
  if (params.page && params.pageSize) {
    sql += " limit ?,?";
    sqlArr.push((params.page - 1) * params.pageSize, parseInt(params.pageSize));
  }
  var callBack = async (err, data) => {
    if (err) {
      console.log(err);
      res.send({
        code: 400,
        msg: "模糊查询失败"
      });
    } else {
      res.send({
        code: 200,
        msg: "模糊查询成功",
        data: data,
      });
    }
  };
  db.sqlConnect(sql, sqlArr, callBack);
};

module.exports = {
  getAssetsInfo,
  addAssetsInfo,
  deleteAssets,
  updateAssetsInfo,
  updateAssetsStatus,
  getAssetsStatusTotal
};
